import { defineConfig, loadEnv } from "vite";
import react from "@vitejs/plugin-react";
import path from "path";
import { createHtmlPlugin } from "vite-plugin-html";
import * as fs from "node:fs";

const getTarget = (mode: string, target: string) => {
  return loadEnv(mode, process.cwd())[target];
};

// https://vitejs.dev/config/
export default ({ mode }: any) =>
  defineConfig({
    server: {
      host: "0.0.0.0",
      port: 8888,
      // https: {
      //   cert: fs.readFileSync(path.join(__dirname, "keys/cert.crt")),
      //   key: fs.readFileSync(path.join(__dirname, "keys/cert.key")),
      // },
    },
    plugins: [
      react(),
      createHtmlPlugin({
        inject: {
          data: {
            title: getTarget(mode, "VITE_APP_TITLE"),
          },
        },
      }),
    ],
    resolve: {
      alias: {
        "@": path.resolve(__dirname, "src"), // 路径别名
      },
      extensions: [".js", ".ts", ".json", ".tsx"], // 导入时想要省略的扩展名列表
    },
    css: {
      preprocessorOptions: {
        scss: {
          additionalData:
            "@import './src/assets/scss/theme.scss';@import './src/assets/scss/global.scss';",
        },
      },
    },
  });
